package com.mifengs.mall.receiver;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.google.gson.Gson;
import com.mifengs.commonbase.util.PreferenceUtils;
import com.mifengs.mall.entity.PullMsgBean;
import com.mifengs.mall.ui.main.MainActivity;
import com.mifengs.mall.ui.push.PushDialogActivity;
import com.mifengs.mall.util.ToIntentUtil;
import com.tencent.android.tpush.XGPushBaseReceiver;
import com.tencent.android.tpush.XGPushClickedResult;
import com.tencent.android.tpush.XGPushRegisterResult;
import com.tencent.android.tpush.XGPushShowedResult;
import com.tencent.android.tpush.XGPushTextMessage;

import org.json.JSONException;
import org.json.JSONObject;

import static com.mifengs.mall.util.UIUtil.getContext;
import static com.tencent.android.tpush.common.Constants.LogTag;

/**
 * Created by lynn on 2018/1/10.
 */

public class XGMsgReceiver extends XGPushBaseReceiver{

    private XGMsgThead msgThead;
    private  PullMsgBean pullMsgBean;
    private Context mContext;
    // 语音合成对象
    @Override
    public void onRegisterResult(Context context, int i, XGPushRegisterResult xgPushRegisterResult) {

    }

    @Override
    public void onUnregisterResult(Context context, int i) {

    }

    @Override
    public void onSetTagResult(Context context, int i, String s) {

    }

    @Override
    public void onDeleteTagResult(Context context, int i, String s) {

    }

    // 消息透传
    @Override
    public void onTextMessage(Context context, XGPushTextMessage message) {

        String text = "收到消息:" + message.toString();
        // 获取自定义key-value
        String customContent = message.getCustomContent();
        if (customContent != null && customContent.length() != 0) {
            try {
                JSONObject obj = new JSONObject(customContent);
                // key1为前台配置的key
                if (!obj.isNull("key")) {
                    String value = obj.getString("key");
                    Log.d(LogTag, "get custom value:" + value);
                }
                // ...
            } catch (JSONException e) {
                e.printStackTrace();
            }
            context.startActivity(new Intent(getContext(),PushDialogActivity.class ));
        }
        // APP自主处理消息的过程...
        Log.d(LogTag, text);
    }
    // 通知点击回调 actionType=1为该消息被清除，actionType=0为该消息被点击
    @Override
    public void onNotifactionClickedResult(Context context, XGPushClickedResult message) {
        if (context == null || message == null) {
            return;
        }
        mContext = context;
        // 获取自定义key-value
        String ster = message.getCustomContent();
        // 初始化合成对象
        try {
             pullMsgBean =new Gson().fromJson(ster,PullMsgBean.class);
            msgThead = new XGMsgThead();
            msgThead.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // APP自主处理的过程。。。
//        Log.d(LogTag, text);
    }

    @Override
    public void onNotifactionShowedResult(Context context, XGPushShowedResult xgPushShowedResult) {
    }

    private void toIntent(){
        try {
            if(pullMsgBean.getType().equals("1")){
                if(!"".equals(pullMsgBean.getAppPage())){
                    if(!pullMsgBean.getAppPage().equals("0")){
                        Intent  intent = new Intent(mContext,MainActivity.class);
                        intent.putExtra("isPush",true);
                        int page = Integer.parseInt(pullMsgBean.getAppPage());
                        intent.putExtra("msgPage",page);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(intent);
                    }
                }
            }else  if(pullMsgBean.getType().equals("2")){
                if(!"".equals(pullMsgBean.getRedirectUrl())){
                    ToIntentUtil.toCommonIntent(pullMsgBean.getRedirectUrl(),mContext);
                }
            }else  if(pullMsgBean.getType().equals("3")){
                if(PreferenceUtils.getLoginStatus()){
                    ToIntentUtil.toCommonIntent(pullMsgBean.getRedirectUrl(),mContext);
                }
            }
        }catch (Exception e){

        }

    }

    Handler mHandle = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    toIntent();
                    if (msgThead != null) {
                        if (msgThead.isAlive()) {
                            msgThead.interrupt();
                        }
                    }
                    break;
            }
        }
    };

    public class XGMsgThead extends Thread {
        @Override
        public void run() {
            try {
                Thread.sleep(4000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Message msg = new Message();
            msg.what = 1;
            mHandle.sendMessage(msg);
        }
    }
}
